<?php

namespace App\Console\Commands;

use App\Models\DayStatisticsModel;
use App\Models\DistrictModel;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;

class DayStatistics extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:day-statistics {day?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计数据';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $day = $this->argument('day')??now()->subDay()->format('Y-m-d');
        $newUser = User::query()->whereDate('created_at',$day)->count();
          // 查询指定日期注册并在当天报名的用户总数
        $new_active_user = User::whereDate('user.created_at', $day)
            ->join('sign_up_job_position', 'user.id', '=', 'sign_up_job_position.user_id')
            ->whereDate('sign_up_job_position.created_at', $day)
            ->distinct('user.id')
            ->count('user.id');
        DayStatisticsModel::query()->updateOrCreate(
            ['day'=>$day],
            [
                'new_user'=> $newUser,
                'new_active_user'=> $new_active_user,
            ]
        );
        $this->info('统计结束');
    }
}